import { createMemoryHistory, createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Demo1 from '../views/Demo1.vue'
import Demo2 from '../views/Demo2.vue'
import Demo3 from '../views/Demo3.vue'
import Dashboard from '../views/Demo4.vue'
import Login from '../views/Login/Login.vue'
import Table from '@/views/Table/Table.vue'
import Table2 from '@/views/Table2/Table.vue'
import api from '@/api/routerApi'


const localRouter = [{
  path: '/',
  name: 'Home',
  component: Home,
  children: []
}, {
  path: '/Login',
  name: 'login',
  component: Login
}];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHistory(),
  routes: localRouter
});

//必须异步不然添加进路由路由就被加载了
await api.getRouter().then(res => {
  if (res.success) {
    res.datas.forEach(route => {
      if (route.children.length > 0) {
        route.children.forEach(x => {
          const vueRoute = {
            path: x.path,
            name: x.name,
            component: () => import(x.component  /* @vite-ignore */),
            children: []
          };
          // 添加到Home路由的children中
          router.addRoute("Home", vueRoute);
        });
      } else {
        const vueRoute = {
          path: route.path,
          name: route.name,
          component: () => import(route.component  /* @vite-ignore */),
          children: []
        };
        // 直接添加到Home路由下
        router.addRoute("Home", vueRoute);
      }
    });
  }
});






export default router
